﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace YJKRevitKernel.FaceCalculateUtility
{
	public class FaceGroupHandler
	{
		internal FaceGroupHandler(List<BasePacker> lstBasePacker, int inputXNumber, int inputYNumber, int inputZNumber)
		{
			this.m_nXNumbe = inputXNumber;
			this.m_nYNumbe = inputYNumber;
			this.m_nZNumbe = inputZNumber;
			this.m_lstAllSpiltBoxPacker = this.MakeSplitBoxPacker(lstBasePacker);
		}

		internal List<FaceLinkeHanlder> GroupFacePacker(List<PlanarFacePacker> input)
		{
			if (input == null)
			{
				input = new List<PlanarFacePacker>();
			}
			List<FaceLinkeHanlder> list = new List<FaceLinkeHanlder>();
			Dictionary<SplitBoxPacker, List<PlanarFacePacker>> dictionary = (from n in this.m_lstAllSpiltBoxPacker
			select new KeyValuePair<SplitBoxPacker, List<PlanarFacePacker>>(n, new List<PlanarFacePacker>())).ToDictionary((KeyValuePair<SplitBoxPacker, List<PlanarFacePacker>> k) => k.Key, (KeyValuePair<SplitBoxPacker, List<PlanarFacePacker>> k) => k.Value);
			foreach (PlanarFacePacker planarFacePacker in input)
			{
				List<SplitBoxPacker> list2 = new List<SplitBoxPacker>();
				foreach (SplitBoxPacker splitBoxPacker in this.m_lstAllSpiltBoxPacker)
				{
					if (planarFacePacker.IfInSplitBoxPacker(splitBoxPacker))
					{
						list2.Add(splitBoxPacker);
					}
				}
				bool flag = list2.Count > 1;
				foreach (SplitBoxPacker key in list2)
				{
					PlanarFacePacker item = flag ? new PlanarFacePacker(planarFacePacker) : planarFacePacker;
					dictionary[key].Add(item);
				}
			}
			foreach (KeyValuePair<SplitBoxPacker, List<PlanarFacePacker>> keyValuePair in dictionary)
			{
				list.Add(new FaceLinkeHanlder(keyValuePair.Value, false));
			}
			return list;
		}

		private List<SplitBoxPacker> MakeSplitBoxPacker(List<BasePacker> lstBasePacker)
		{
			List<SplitBoxPacker> list = new List<SplitBoxPacker>();
			XYZ xyz = null;
			XYZ xyz2 = null;
			List<Element> list2 = new List<Element>();
			List<Solid> list3 = new List<Solid>();
			bool flag = true;
			foreach (BasePacker basePacker in lstBasePacker)
			{
				if (!(basePacker is ElementPacker))
				{
					list3.Add((basePacker as SolidPacker).ThisSolid);
					flag = false;
				}
				else
				{
					list2.Add((basePacker as ElementPacker).ThisElement);
					flag = true;
				}
			}
			if (flag)
			{
				GeometryHelper.GetBulidingMaxAndMin(list2, out xyz, out xyz2);
				if (xyz == null && xyz2 == null)
				{
					return null;
				}
			}
			else
			{
				GeometryHelper.GetBulidingMaxAndMin(list3, out xyz, out xyz2);
				if (xyz == null && xyz2 == null)
				{
					return null;
				}
			}
			List<List<XYZ>> list4 = null;
			List<double> list5 = null;
			GeometryHelper.GetBuildingCutPoint(xyz, xyz2, this.m_nXNumbe, this.m_nYNumbe, this.m_nZNumbe, out list4, out list5);
			if (list4 != null && list5 != null)
			{
				new List<SplitBoxPacker>();
				for (int i = 0; i < list5.Count - 1; i++)
				{
					for (int j = 0; j < list4.Count - 1; j++)
					{
						for (int k = 0; k < list4[j].Count - 1; k++)
						{
							XYZ inputMin = new XYZ(list4[j][k].X, list4[j][k].Y, list5[i]);
							SplitBoxPacker item = new SplitBoxPacker(new XYZ(list4[j + 1][k + 1].X, list4[j + 1][k + 1].Y, list5[i + 1]), inputMin);
							list.Add(item);
						}
					}
				}
			}
			return list;
		}

		private int m_nXNumbe;

		private int m_nYNumbe;

		private int m_nZNumbe;

		public List<SplitBoxPacker> m_lstAllSpiltBoxPacker;
	}
}
